﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushRewardPage : Page 
    {
        public UIGrid grid;
        public GameObject itemPrefab;

        List<GameObject> items = new List<GameObject>();

        protected override void DoOpen()
        {
            Init(StaticData.sectionReward.gain);
        }

        protected override void DoClose()
        {
            Clear();
            StaticData.sectionReward = null;
        }

        public void Clear() {
            foreach (var item in items) {
                GameObject.Destroy(item);
            }
            items.Clear();
        }

        public void Init(ProtoBuf.Message.ResultObject gain) {
            foreach (var info in gain.wars) {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.id, true);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.equips) {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.equipId, true);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.treas)
            {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.treasureId, true);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.simps)
            {
                if (info.num == 0)
                    continue;
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.id, true, info.num);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }

            grid.repositionNow = true;
        }

    }
}